import { getFoodDetail } from '../../apis/food';
import { addShop } from '../../apis/cart';

Page({
  data: {
    detailInfo: {},
    cartItemCount: 0, // 购物车商品数量
    showRedDot: false // 控制小红点的显示
  },

  onLoad(options) {
    const { itemId } = options;
    this.getFoodDetailData(itemId);
  },

  // 跳转到购物车
  goShop() {
    wx.switchTab({
      url: '/pages/myCart/index'
    });
  },

   // 加入购物车
   async addShop() {
    const { name, pic, type, description, price } = this.data.detailInfo;
    const res = await addShop(name, pic, type, description, price);
    if (res.status === 200) {
      wx.showToast({
        title: '添加成功',
        icon: 'none'
      });
      this.setData({ showRedDot: true }); // 显示小红点
    }
  },

  // 获取详情数据
  async getFoodDetailData(id) {
    const res = await getFoodDetail(id);
    if (res.status === 200) {
      this.setData({
        detailInfo: res.data[0]
      });
    }
  }
});
